package com.ahs.region.config;

import java.io.IOException;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.ahs.region.mapper.UserMapper;
import com.alibaba.druid.pool.DruidDataSource;

/**
 * 配置数据源，读取其他额外配置到程序
 * @author dw
 * @version 1.0.0
 */
@Configuration
public class MyBatisConfiguration {

	private static final Logger logger = LoggerFactory.getLogger(MyBatisConfiguration.class);

	@Autowired
	CustomDataSourceProperties properties;

	@Autowired
	SqlSessionFactory sqlSessionFactory;

	@Autowired
	SqlSessionTemplate sessionTemplate;

	@Bean
	public SqlSessionTemplate sqlSessionTemplate() {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
	
	@Bean
	public UserMapper regionMapper() {
		return sessionTemplate.getMapper(UserMapper.class);
	}

	@Bean
	public SqlSessionFactory sqlSessionFactory() {

		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		 DataSourceBuilder builder =
		 DataSourceBuilder.create(this.properties.getClassLoader())
		 .driverClassName(this.properties.getDriverClassName()).url(this.properties.getUrl())
		 .username(this.properties.getUsername()).password(this.properties.getPassword());
		 builder.driverClassName("com.mysql.jdbc.Driver");
		bean.setDataSource(builder.build());
		DruidDataSource datasource = new DruidDataSource();
		datasource.setUrl(this.properties.getUrl());
		datasource.setUsername(this.properties.getUsername());
		datasource.setPassword(this.properties.getPassword());
		datasource.setDriverClassName(this.properties.getDriverClassName());
		datasource.setInitialSize(this.properties.getInitialSize());
		datasource.setMinIdle(this.properties.getMinIdle());
		datasource.setMaxActive(this.properties.getMaxActive());
		datasource.setTimeBetweenEvictionRunsMillis(this.properties.getTimeBetweenEvictionRunsMillis());
		datasource.setValidationQuery(this.properties.getValidationQuery());
		datasource.setTestWhileIdle(this.properties.getTestWhileIdle());
		datasource.setTestOnBorrow(this.properties.getTestOnBorrow());
		datasource.setTestOnReturn(this.properties.getTestOnReturn());
		datasource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=10000");
		bean.setDataSource(datasource);

		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

		try {
			bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
		} catch (IOException e1) {
			logger.info("绑定mapper.xml失败");
		}

		SqlSessionFactory factory = null;
		try {
			factory = bean.getObject();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return factory;
	}


}
